package com.spring.Controller;

import com.spring.Controller.Interface.LogController;
import com.spring.Entity.LogEntity;
import com.spring.Service.Interface.LogService;
import com.spring.Util.RSAUtils;
import org.apache.commons.codec.binary.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.interfaces.RSAPublicKey;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Map;

/**
 * Created by Arabira on 2016/8/4.
 */
@Controller
@RequestMapping("/log")
public class LogControllerImpl implements LogController{
    @Autowired
    private LogService logService;

    public Map prepare(Map map) {
        RSAPublicKey publicKey = RSAUtils.getDefaultPublicKey();
        map.put("modulus" ,new String( Hex.encodeHex( publicKey.getModulus().toByteArray() ) ));
        map.put("exponent", new String(Hex.encodeHex(publicKey.getPublicExponent().toByteArray())));
        return map;
    }

    //TODO：shiro验证身份，AOP记录日志
    @ResponseBody
    @RequestMapping("/search")
    public Map<String, Object> findLogs(Integer pageNum, Integer pageSize) {
        Map<String, Object> response = logService.findLogs(pageNum, pageSize);
        return response;
    }

    @ResponseBody
    @RequestMapping("/searchByAdmin")

    public Map<String, Object> findLogsAll(HttpSession session, HttpServletRequest request) {
        Map<String, Object> response = logService.findLogsAll();
        return response;
    }

    @ResponseBody
    @RequestMapping("/searchReqByAdmin")

    public Map<String, Object> findReqLogsAll(String userName,String IP,String Oper, String timeS, String timeE,HttpSession session,HttpServletRequest request) {

        if(timeS!=""){
            timeS= timeS+" 00:00:00";
        }else{
            timeS = "2000-01-01 00:00:00";
        }
        if(timeE!=""){
            timeE = timeE+" 00:00:00";
        }else{
            timeE = "9999-12-31 23:59:59";
        }

        Map<String, Object> response = logService.findReqLogsAll(userName,IP,Oper,timeS,timeE);
        return response;
    }
}
